﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Threading;

namespace DrawTest
{
	public class FarLine
	{
		public FarLine(){
			normalColor = ColorTranslator.FromHtml(Consts.FarLineColor);
			
			isReference = false;
			stableAngle = 0;
		}
		
		public void draw(int currentAngle, Graphics dc){
			int term = 0;
			if ( currentAngle <= 60 && stableAngle >= 300){
				term = 360;
			}
			
			int delta = stableAngle - currentAngle - term;
			bool greenLight = false;
			
			if (delta <= 0 && delta >= -60 ){
				greenLight = true;
			}
			
			if (greenLight){
				
				Color color = normalColor;
				int width = 1;
				if (isReference){
					width = 2;
				}
				Pen p = new Pen(color, width);// цвет линии и ширина
				
				
				disappearanceLine(currentAngle,delta);
				
				dc.RotateTransform(stableAngle - currentAngle);
				
				Point p1 = new Point(0,0);// первая точка
				Point p2 = new Point(0,-Consts.FarLineLenght);// вторая точка
				dc.CompositingQuality = CompositingQuality.GammaCorrected;
				dc.DrawLine(p, p1, p2);// рисуем линию
				
				dc.RotateTransform(currentAngle - stableAngle);
			}
		}
		
		private void disappearanceLine(int currentAngle,int delta){
			bool greenLight = false;
			
			setTransparancy(255);
			
			if (delta <= -10 && delta >= -60 ){
				greenLight = true;
			}
			
			int rate = 50-((-delta)-10);
			
			if (greenLight){
				setTransparancy(rate*4);
				
			}
			
		}
		
		private void setTransparancy(int value){//0..255
			Color tempColor = Color.FromArgb(value, normalColor.R, normalColor.G, normalColor.B);
			normalColor = tempColor;
		}
		
		public void setStableAngle(int stableAngle2){
			stableAngle = stableAngle2;
		}
		
		public void setReference(){
			isReference = true;
		}

		private Color normalColor;
		
		private int stableAngle;
		private bool isReference;
		
	}
}
